// 生成器函数 Generator

// function * foo(){
//     console.log('foo')
//     return 100
// }
// const result = foo()
// console.log(result.next())

function * foo(){
    console.log(11111)
    yield 100
    console.log(2222)
    yield 200
    console.log(3333)
    yield 300
}
const generator = foo()
console.log(generator.next())
console.log(generator.next())
console.log(generator.next())
console.log(generator.next())


// ---------------------------------
// 生成器的应用 Generator

// 案例一: 发号器

console.log('-----------------------------')
// function * createIdMaker(){
//     let id = 1
//     while(true){
//         yield id++
//     }
// }
// const idMaker = createIdMaker()
// console.log(idMaker.next().value)
// console.log(idMaker.next().value)
// console.log(idMaker.next().value)
// console.log(idMaker.next().value)
// console.log(idMaker.next().value)

// 案例二: 使用Generator函数实现iterator接口
const todos = {
    life: ['吃饭', '睡觉', '打豆豆'],
    learn: ['语文', '数学', '英语'],
    work: ['喝茶'],
    [Symbol.iterator]: function *(){
        const all = [...this.life, ...this.learn, ...this.work]
        for(const item of all){
            yield item
        }
    }
}

for(const item of todos){
    console.log(item,'yield')
}


















